<template>
  <el-dialog
      :title="form.id ? '编辑学习路径' : '新增学习路径'"
      :visible.sync="show"
      width="600px"
      @close="onClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="路径名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入路径名称"/>
      </el-form-item>
      <el-form-item label="专业领域" prop="domain">
        <el-select v-model="form.domain" placeholder="请选择专业领域" style="width: 100%">
          <el-option label="铁道工程" :value="1"/>
          <el-option label="信号控制" :value="2"/>
          <el-option label="机车车辆" :value="3"/>
        </el-select>
      </el-form-item>
      <el-form-item label="路径描述" prop="description">
        <el-input
            type="textarea"
            v-model="form.description"
            :rows="4"
            placeholder="请输入路径描述"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { http } from "@/api";

export default {
  name: "LearningPathAdd",
  data() {
    return {
      show: false,
      form: {
        id: null,
        name: '',
        domain: '',
        description: '',
        status: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入路径名称', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
        ],
        domain: [
          { required: true, message: '请选择专业领域', trigger: 'change' }
        ],
        description: [
          { required: true, message: '请输入路径描述', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    open(row) {
      this.show = true
      if (row) {
        this.form = { ...row }
      } else {
        this.form = {
          id: null,
          name: '',
          domain: '',
          description: '',
          status: 1
        }
      }
    },
    onClose() {
      this.show = false
      this.$refs.form.resetFields()
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const data = { ...this.form }
          if (data.id) {
            http.resourcePath.update(data).then(() => {
              this.$message.success('编辑成功')
              this.onClose()
              this.$emit('get-page')
            })
          } else {
            http.resourcePath.create(data).then(() => {
              this.$message.success('新增成功')
              this.onClose()
              this.$emit('get-page')
            })
          }
        }
      })
    }
  }
}
</script>
